package org.neurone.action.userspace;

import org.neurone.action.BaseActionSupport;
import org.neurone.form.UserInfoForm;
import org.neurone.userspace.userinfo.UserFace;
import org.neurone.userspace.userinfo.UserMgr;
import org.neurone.userspace.userinfo.impl.UserFaceLocal;
import org.neurone.userspace.userinfo.impl.UserFaceLocalFactory;

public class UserInfoModAction extends BaseActionSupport {
	private UserInfoForm user;
	private UserMgr userMgr;
	private UserFaceLocalFactory userFaceFactory;
	private Long userid;
	private String passwordagain;
	
	public UserInfoForm getUser() {
		return user;
	}

	public UserMgr getUserMgr() {
		return userMgr;
	}

	public void setUser(UserInfoForm user) {
		this.user = user;
	}

	public void setUserMgr(UserMgr userMgr) {
		this.userMgr = userMgr;
	}


	public UserFaceLocalFactory getUserFaceFactory() {
		return userFaceFactory;
	}

	public void setUserFaceFactory(UserFaceLocalFactory userFaceFactory) {
		this.userFaceFactory = userFaceFactory;
	}

	public Long getUserid() {
		return userid;
	}

	public void setUserid(Long userid) {
		this.userid = userid;
	}
	
	public String getPasswordagain() {
		return passwordagain;
	}

	public void setPasswordagain(String passwordagain) {
		this.passwordagain = passwordagain;
	}

	private String newUser()
	{
		if(user !=null)
		{
			this.userid = userMgr.newUser(this.user);
			return success();
		}
		else
		{
			return INPUT;
		}
	}
	private String updateUser()
	{
		if(user !=null)
		{
			UserFaceLocal u = userFaceFactory.getNewUserFace(this.userid);
			
			u.setEmail(this.user.getEmail());
			u.setImage(this.user.getImage());
			u.setRealName(this.user.getRealName());
			u.setStatus(this.user.getStatus());
			u.setUserName(this.user.getUserName());
			u.setUserNick(this.user.getUserNick());
			
			if(! this.user.getPasswd().equals(u.getPasswd()))
			{
				u.setPasswd(this.user.getPasswd());
			}
			
			u.flush();
			
			return success();
		}
		else
		{
			return INPUT;
		}
	}
	
	public String modUser()
	{
		if(userid !=null)
		{
			return updateUser();
		}
		else
		{
			return newUser();
		}
	}	
}
